package cn.bluech.lib.ui.gesture;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;

/**
 * Description:
 * Create by Codeant on 2018/10/16.
 */
public class PlayerGestureDetector {
    private static final int TYPE_NONE = 0;
    private static final int TYPE_VOLUME = 1;
    private static final int TYPE_BRIGHTNESS = 2;
    private static final int TYPE_SEEK = 3;
    private Context context;
    private int type;
    private int seekPos;
    private boolean isEnable;
    private GestureDetector gestureDetector;
    private View view;
    private Listener listener;

    public PlayerGestureDetector(@NonNull Context context, @NonNull View targetView, Listener l) {
        this.context = context;
        this.view = targetView;
        this.listener = l;
        this.isEnable = true;
        gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
            /**
             * 单击
             */
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e){
                if(!isEnable)
                    return true;
                if(null!=listener)
                    listener.onSingleTap();
                return true;
            }

            @Override
            public boolean onDown(MotionEvent e) {
                type = TYPE_NONE;
                return super.onDown(e);
            }

            @Override
            public boolean onScroll(MotionEvent start, MotionEvent end, float distanceX, float distanceY) {
                if(!isEnable)
                    return false;
                if(null==listener)
                    return false;

                if(type==TYPE_NONE) {
                    if (Math.abs(distanceX) < Math.abs(distanceY)) {
                        // 垂直滑动
                        if (start.getX() < view.getWidth() / 2)
                            type = TYPE_BRIGHTNESS; // 左半屏为调亮度
                        else
                            type = TYPE_VOLUME;      // 右半屏为调音量
                    } else
                        type = TYPE_SEEK;             // 水平滑动为调进度
                }

                switch (type){
                    case TYPE_BRIGHTNESS:
                        listener.onBrightnessSlide((int)((start.getY()-end.getY())*100/view.getHeight()));
                        break;
                    case TYPE_VOLUME:
                        listener.onVolumeSlide((int)((start.getY()-end.getY())*100/view.getHeight()));
                        break;
                    case TYPE_SEEK:
                        seekPos = listener.onProgressSlide((int)((end.getX()-start.getX())*100/view.getWidth()));
                        break;
                    default:
                        break;
                }

                return super.onScroll(start, end, distanceX, distanceY);
            }
        });

        view.setOnTouchListener((View v, MotionEvent event) ->{
            gestureDetector.onTouchEvent(event);
            if(event.getAction()==MotionEvent.ACTION_UP){
                v.performClick(); // 去除不能触发onClick()的警告

                if(null!=listener){
                    if(type==TYPE_SEEK)
                        listener.onSeek(seekPos);
                    listener.onDetectorEnd();
                }
            }
            return true;
        });
    }

    public interface Listener{
        void onSingleTap();
        void onVolumeSlide(int percent);
        void onBrightnessSlide(int percent);
        int onProgressSlide(int percent);
        void onSeek(int pos);
        void onDetectorEnd();
    }
}
